package data.dictionary;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import data.preprocessing.*;
import data.set.OfflineSetIterator;
import data.set.implementation.OfflineSetIterator_FromIds;



/**
 * Builds a dictionary into memory
 * @author guillaumes
 *
 * @param <X> : the dictionary
 */
public class Dictionary_Memory<X> implements Dictionary<X>
{
	protected HashMap<X,Integer> inverseIndex;
	protected ArrayList<X> index;
	protected ArrayList<Integer> countWords;
	//protected StringProcessor sp;
	
	public Dictionary_Memory()
	{
		inverseIndex=new HashMap<X,Integer>();
		index=new ArrayList<X>();
		countWords = new ArrayList<Integer>();
		//this.sp = sp;
	}
	
	

	
	public X getObject(int i) 
	{
		return(index.get(i));
	}

	
	public int indexOf(X x) {
		if (inverseIndex.containsKey(x))
			return(inverseIndex.get(x));
		return(-1);
	}

	
	public final long size()
	{
		return index.size();
	}

	
	public final Iterator<X> iterator() {
		return(index.iterator());
	}
	
	public void addValue(X x)
	{
		int i = indexOf(x); 
		if (i==-1)
		{
			index.add(x);
			countWords.add(1);
			inverseIndex.put(x,index.size()-1);
		}
		else
			countWords.set(i, countWords.get(i)+1);
			
	}


	public OfflineSetIterator<X> offlinesetiterator() {
		// TODO Auto-generated method stub
		return null;
	}




	/**
	 * @return the countWords
	 */
	public ArrayList<Integer> getCountWords() {
		return countWords;
	}




	public OfflineSetIterator<X> inputsetiterator() {
		ArrayList<Integer> tab=new ArrayList<Integer>();
		for(int i=0;i<size();i++)
			tab.add(i);
		return(new OfflineSetIterator_FromIds<X>(tab,this));
	}

}
